#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;

#define rep(i,n) for (int i=0; i<n; i++)

#define TRACE(x...) 
#define PRINT(x...) TRACE(printf(x))

#define INF 0x3f3f3f3f

char a[100010];
char b[100010];
int n;
int ch[30][30];
char s, d;
int vs, vd;
int c;
int la, lb;
int sum;
char res[100010];
int ps;
int bps;

int main() {
	scanf(" %s", a);
	scanf(" %s", b);

	scanf("%d", &n);

	rep(i,26) {
		rep(j,26) ch[i][j]=INF;
		ch[i][i]=0;
	}

	rep(i,n) {
		scanf(" %c %c %d", &s, &d, &c);
		vs = s-'a'; vd = d-'a';
	
		ch[vs][vd]= min(ch[vs][vd], c);	
	}

	rep(k,26) rep(i,26) rep(j,26) ch[i][j]=min(ch[i][j], ch[i][k]+ch[k][j]);


	TRACE(
		rep(i,26) {
			rep(j,26) printf("%d ", ch[i][j]==INF? -1 : ch[i][j]);
			printf("\n");
		}
	)

	la = strlen(a);
	lb = strlen(b);

	if (la!=lb) {
		printf("-1\n");
		return 0;
	}

	sum=0;

	rep(i, la) {
		if (a[i]==b[i]) {
			res[i]=a[i];
			continue;
		}
		vs=a[i]-'a'; vd=b[i]-'a';
		bps=INF;

		rep(j,26) {
			ps = ch[vs][j]+ch[vd][j];
		
			if (ps<bps) {
				bps=ps; c=j;
			}
		}

		sum+=bps;
		res[i]='a'+c;

		if (bps==INF) {
			printf("-1\n");
			return 0;
		}	
	}

	res[la]='\0';
	printf("%d\n", sum);
	printf("%s\n", res);
	
	return 0;
}
